package com.tdtxdcxm.hellowebview.slice;

import com.tdtxdcxm.hellowebview.ResourceTable;
import com.tdtxdcxm.hellowebview.webconfig.StartWebView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.webengine.WebView;

public class MainAbilitySlice extends AbilitySlice {

    private TextField WedViewRootdl_ddl1_toptfd;
    private Button WedViewRootdl_ddl1_searchbutton;
    private DirectionalLayout WedViewRootdl_ddl2;

    private WebView subWedViewRootdl_the_webview;
    private Text subNoticerootdl_the_NoticeText;

    private Button WedViewRootdl_ddl3_backbutton,WedViewRootdl_ddl3_rebutton,WedViewRootdl_ddl3_gobutton;



    public void init_noticelayout(DirectionalLayout ddl2){
        ddl2.removeAllComponents();

        DirectionalLayout subNoticerootdl = (DirectionalLayout) LayoutScatter.getInstance(MainAbilitySlice.this).parse(ResourceTable.Layout_ability_sublayoutnotice,null,false);
        subNoticerootdl_the_NoticeText = (Text) subNoticerootdl.findComponentById(ResourceTable.Id_subNoticerootdl_the_NoticeText);
        subNoticerootdl_the_NoticeText.setText("操作提示"+"\n"+"单击---刷新   双击---重置"+"\n"+"长按---加载WebView");
        ddl2.addComponent(subNoticerootdl);
    }

    public void init_MainAbilitySliceComponents(){
        WedViewRootdl_ddl1_toptfd = (TextField) findComponentById(ResourceTable.Id_WedViewRootdl_ddl1_toptfd);
        WedViewRootdl_ddl1_searchbutton = (Button) findComponentById(ResourceTable.Id_WedViewRootdl_ddl1_searchbutton);
        WedViewRootdl_ddl2 = (DirectionalLayout) findComponentById(ResourceTable.Id_WedViewRootdl_ddl2);
        WedViewRootdl_ddl3_backbutton = (Button) findComponentById(ResourceTable.Id_WedViewRootdl_ddl3_backbutton);
        WedViewRootdl_ddl3_rebutton = (Button) findComponentById(ResourceTable.Id_WedViewRootdl_ddl3_rebutton);
        WedViewRootdl_ddl3_gobutton = (Button) findComponentById(ResourceTable.Id_WedViewRootdl_ddl3_gobutton);

        init_noticelayout(WedViewRootdl_ddl2);

        WedViewRootdl_ddl1_searchbutton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                WedViewRootdl_ddl1_toptfd.clearFocus();
                if(StartWebView.setwebviewstatus == true) {
                    subWedViewRootdl_the_webview.load(WedViewRootdl_ddl1_toptfd.getText());
                }
            }
        });
        WedViewRootdl_ddl3_backbutton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                WedViewRootdl_ddl1_toptfd.clearFocus();
                if(StartWebView.setwebviewstatus == true && subWedViewRootdl_the_webview.getNavigator().canGoBack()){
                    subWedViewRootdl_the_webview.getNavigator().goBack();
                }
            }
        });

        /****************************************给底部中间按钮设置三个监听器************************************/
        WedViewRootdl_ddl3_rebutton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                WedViewRootdl_ddl1_toptfd.clearFocus();
                if(StartWebView.setwebviewstatus == true){
                    subWedViewRootdl_the_webview.reload();
                }
            }
        });
        WedViewRootdl_ddl3_rebutton.setDoubleClickedListener(new Component.DoubleClickedListener() {
            @Override
            public void onDoubleClick(Component component) {
                WedViewRootdl_ddl2.removeAllComponents();
                StartWebView.setwebviewstatus = false;
                WedViewRootdl_ddl1_toptfd.clearFocus();
                WedViewRootdl_ddl1_toptfd.setText("");
                init_noticelayout(WedViewRootdl_ddl2);
            }
        });
        WedViewRootdl_ddl3_rebutton.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                if(WedViewRootdl_ddl2.findComponentById(ResourceTable.Id_subWedViewRootdl_the_webview) == null) {
                    WedViewRootdl_ddl2.removeAllComponents();
                    DirectionalLayout subWedViewRootdl = (DirectionalLayout) LayoutScatter.getInstance(MainAbilitySlice.this).parse(ResourceTable.Layout_ability_sublayoutwebview, null, false);

                    subWedViewRootdl_the_webview = (WebView) subWedViewRootdl.findComponentById(ResourceTable.Id_subWedViewRootdl_the_webview);

                    WedViewRootdl_ddl2.addComponent(subWedViewRootdl);
                    StartWebView.startWebview(subWedViewRootdl_the_webview, "https://cn.bing.com/");
                    subWedViewRootdl_the_webview.load("https://cn.bing.com/");
                }
            }
        });
        /****************************************给底部中间按钮设置三个监听器************************************/

        WedViewRootdl_ddl3_gobutton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                WedViewRootdl_ddl1_toptfd.clearFocus();
                if(StartWebView.setwebviewstatus == true && subWedViewRootdl_the_webview.getNavigator().canGoForward()){
                    subWedViewRootdl_the_webview.getNavigator().goForward();
                }
            }
        });
    }


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        init_MainAbilitySliceComponents();
    }

    @Override
    public void onInactive() {
        super.onInactive();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onBackground() {
        super.onBackground();
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}
